﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Framework.BasicFramework;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Core.IMessage;
using IPC.Communication.Framework.Core.Net;
using IPC.Communication.Framework.Reflection;
using IPC.Communication.Framework.Core.Types;

namespace IPC.Communication.Framework.ModBus
{
    public class ModbusTcpNet : NetworkDeviceBase, IModbus, IReadWriteDevice, IReadWriteNet
    {
        private byte station = 1;

        private readonly SoftIncrementCount softIncrementCount;

        private bool isAddressStartWithZero = true;

        public bool AddressStartWithZero
        {
            get
            {
                return isAddressStartWithZero;
            }
            set
            {
                isAddressStartWithZero = value;
            }
        }

        public byte Station
        {
            get
            {
                return station;
            }
            set
            {
                station = value;
            }
        }

        public DataFormat DataFormat
        {
            get
            {
                return base.ByteTransform.DataFormat;
            }
            set
            {
                base.ByteTransform.DataFormat = value;
            }
        }

        public bool IsStringReverse
        {
            get
            {
                return base.ByteTransform.IsStringReverseByteWord;
            }
            set
            {
                base.ByteTransform.IsStringReverseByteWord = value;
            }
        }

        public SoftIncrementCount MessageId
        {
            get { return softIncrementCount; }
        }

        public ModbusTcpNet()
        {
            softIncrementCount = new SoftIncrementCount(65535L, 0L);
            base.WordLength = 1;
            station = 1;
            base.ByteTransform = new ReverseWordTransform();
        }

        public ModbusTcpNet(string ipAddress, int port = 502, byte station = 1)
        {
            softIncrementCount = new SoftIncrementCount(65535L, 0L);
            IpAddress = ipAddress;
            Port = port;
            base.WordLength = 1;
            this.station = station;
            base.ByteTransform = new ReverseWordTransform();
        }

        protected override INetMessage GetNewNetMessage()
        {
            return new ModbusTcpMessage();
        }

        protected override OperateResult InitializationOnConnect(Socket socket)
        {
            if (isUseAccountCertificate)
            {
                return AccountCertificate(socket);
            }
            return base.InitializationOnConnect(socket);
        }

        //[AsyncStateMachine(typeof(<InitializationOnConnectAsync>d__7))]
        //[DebuggerStepThrough]
        //protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
        //{
        //    <InitializationOnConnectAsync>d__7 stateMachine = new <InitializationOnConnectAsync>d__7();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.socket = socket;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("ReadInt32Array", "")]
        public override OperateResult<int[]> ReadInt32(string address, ushort length)
        {
            IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * base.WordLength * 2)), (byte[] m) => transform.TransInt32(m, 0, length));
        }

        [HslMqttApi("ReadUInt32Array", "")]
        public override OperateResult<uint[]> ReadUInt32(string address, ushort length)
        {
            IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * base.WordLength * 2)), (byte[] m) => transform.TransUInt32(m, 0, length));
        }

        [HslMqttApi("BatchRead", "批量读取")]
        public OperateResult<Dictionary<string, object>> BatchRead(Dictionary<string, DataTypeEnum> addresses, int batchNumber)
        {
            return new OperateResult<Dictionary<string, object>>(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("BatchWrite", "批量写入")]
        public OperateResult BatchWrite(Dictionary<string, object> addresses, int batchNumber)
        {
            return new OperateResult(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("ReadFloatArray", "")]
        public override OperateResult<float[]> ReadFloat(string address, ushort length)
        {
            IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * base.WordLength * 2)), (byte[] m) => transform.TransSingle(m, 0, length));
        }

        [HslMqttApi("ReadInt64Array", "")]
        public override OperateResult<long[]> ReadInt64(string address, ushort length)
        {
            IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * base.WordLength * 4)), (byte[] m) => transform.TransInt64(m, 0, length));
        }

        [HslMqttApi("ReadUInt64Array", "")]
        public override OperateResult<ulong[]> ReadUInt64(string address, ushort length)
        {
            IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * base.WordLength * 4)), (byte[] m) => transform.TransUInt64(m, 0, length));
        }

        [HslMqttApi("ReadDoubleArray", "")]
        public override OperateResult<double[]> ReadDouble(string address, ushort length)
        {
            IByteTransform transform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * base.WordLength * 4)), (byte[] m) => transform.TransDouble(m, 0, length));
        }

        [HslMqttApi("WriteInt32Array", "")]
        public override OperateResult Write(string address, int[] values)
        {
            IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return Write(address, byteTransform.TransByte(values));
        }

        [HslMqttApi("WriteUInt32Array", "")]
        public override OperateResult Write(string address, uint[] values)
        {
            IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return Write(address, byteTransform.TransByte(values));
        }

        [HslMqttApi("WriteFloatArray", "")]
        public override OperateResult Write(string address, float[] values)
        {
            IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return Write(address, byteTransform.TransByte(values));
        }

        [HslMqttApi("WriteInt64Array", "")]
        public override OperateResult Write(string address, long[] values)
        {
            IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return Write(address, byteTransform.TransByte(values));
        }

        [HslMqttApi("WriteUInt64Array", "")]
        public override OperateResult Write(string address, ulong[] values)
        {
            IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return Write(address, byteTransform.TransByte(values));
        }

        [HslMqttApi("WriteDoubleArray", "")]
        public override OperateResult Write(string address, double[] values)
        {
            IByteTransform byteTransform = HslHelper.ExtractTransformParameter(ref address, base.ByteTransform);
            return Write(address, byteTransform.TransByte(values));
        }

        //[AsyncStateMachine(typeof(<ReadInt32Async>d__20))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<int[]>> ReadInt32Async(string address, ushort length)
        //{
        //    <ReadInt32Async>d__20 stateMachine = new <ReadInt32Async>d__20();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt32Async>d__21))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<uint[]>> ReadUInt32Async(string address, ushort length)
        //{
        //    <ReadUInt32Async>d__21 stateMachine = new <ReadUInt32Async>d__21();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<uint[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFloatAsync>d__22))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<float[]>> ReadFloatAsync(string address, ushort length)
        //{
        //    <ReadFloatAsync>d__22 stateMachine = new <ReadFloatAsync>d__22();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<float[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt64Async>d__23))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<long[]>> ReadInt64Async(string address, ushort length)
        //{
        //    <ReadInt64Async>d__23 stateMachine = new <ReadInt64Async>d__23();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<long[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt64Async>d__24))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<ulong[]>> ReadUInt64Async(string address, ushort length)
        //{
        //    <ReadUInt64Async>d__24 stateMachine = new <ReadUInt64Async>d__24();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ulong[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadDoubleAsync>d__25))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<double[]>> ReadDoubleAsync(string address, ushort length)
        //{
        //    <ReadDoubleAsync>d__25 stateMachine = new <ReadDoubleAsync>d__25();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<double[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__26))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, int[] values)
        //{
        //    <WriteAsync>d__26 stateMachine = new <WriteAsync>d__26();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__27))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, uint[] values)
        //{
        //    <WriteAsync>d__27 stateMachine = new <WriteAsync>d__27();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__28))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, float[] values)
        //{
        //    <WriteAsync>d__28 stateMachine = new <WriteAsync>d__28();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__29))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, long[] values)
        //{
        //    <WriteAsync>d__29 stateMachine = new <WriteAsync>d__29();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__30))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, ulong[] values)
        //{
        //    <WriteAsync>d__30 stateMachine = new <WriteAsync>d__30();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__31))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, double[] values)
        //{
        //    <WriteAsync>d__31 stateMachine = new <WriteAsync>d__31();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public virtual OperateResult<string> TranslateToModbusAddress(string address, byte modbusCode)
        {
            return OperateResult.CreateSuccessResult(address);
        }

        protected override byte[] PackCommandWithHeader(byte[] command)
        {
            return ModbusInfo.PackCommandToTcp(command, (ushort)softIncrementCount.GetCurrentValue());
        }

        protected override OperateResult<byte[]> UnpackResponseContent(byte[] send, byte[] response)
        {
            return ModbusInfo.ExtractActualData(ModbusInfo.ExplodeTcpCommandToCore(response));
        }

        public OperateResult<bool> ReadCoil(string address)
        {
            return ReadBool(address);
        }

        /// <summary>
        /// 读线圈
        /// </summary>
        /// <param name="address"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public OperateResult<bool[]> ReadCoil(string address, ushort length)
        {
            return ReadBool(address, length);
        }

        /// <summary>
        /// 读离散
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public OperateResult<bool> ReadDiscrete(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadDiscrete(address, 1));
        }

        public OperateResult<bool[]> ReadDiscrete(string address, ushort length)
        {
            return ModbusHelper.ReadBoolHelper(this, address, length, 2);
        }

        [HslMqttApi("ReadByteArray", "")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            return ModbusHelper.Read(this, address, length);
        }

        [HslMqttApi("WriteByteArray", "")]
        public override OperateResult Write(string address, byte[] value)
        {
            return ModbusHelper.Write(this, address, value);
        }

        [HslMqttApi("WriteInt16", "")]
        public override OperateResult Write(string address, short value)
        {
            return ModbusHelper.Write(this, address, value);
        }

        [HslMqttApi("WriteUInt16", "")]
        public override OperateResult Write(string address, ushort value)
        {
            return ModbusHelper.Write(this, address, value);
        }

        [HslMqttApi("WriteMask", "")]
        public OperateResult WriteMask(string address, ushort andMask, ushort orMask)
        {
            return ModbusHelper.WriteMask(this, address, andMask, orMask);
        }

        public OperateResult WriteOneRegister(string address, short value)
        {
            return Write(address, value);
        }

        public OperateResult WriteOneRegister(string address, ushort value)
        {
            return Write(address, value);
        }

        //[AsyncStateMachine(typeof(<ReadCoilAsync>d__60))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<bool>> ReadCoilAsync(string address)
        //{
        //    <ReadCoilAsync>d__60 stateMachine = new <ReadCoilAsync>d__60();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCoilAsync>d__61))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<bool[]>> ReadCoilAsync(string address, ushort length)
        //{
        //    <ReadCoilAsync>d__61 stateMachine = new <ReadCoilAsync>d__61();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadDiscreteAsync>d__62))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<bool>> ReadDiscreteAsync(string address)
        //{
        //    <ReadDiscreteAsync>d__62 stateMachine = new <ReadDiscreteAsync>d__62();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadDiscreteAsync>d__63))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<bool[]>> ReadDiscreteAsync(string address, ushort length)
        //{
        //    <ReadDiscreteAsync>d__63 stateMachine = new <ReadDiscreteAsync>d__63();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__64))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
        //{
        //    <ReadAsync>d__64 stateMachine = new <ReadAsync>d__64();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__65))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, byte[] value)
        //{
        //    <WriteAsync>d__65 stateMachine = new <WriteAsync>d__65();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__66))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, short value)
        //{
        //    <WriteAsync>d__66 stateMachine = new <WriteAsync>d__66();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__67))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, ushort value)
        //{
        //    <WriteAsync>d__67 stateMachine = new <WriteAsync>d__67();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteMaskAsync>d__68))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteMaskAsync(string address, ushort andMask, ushort orMask)
        //{
        //    <WriteMaskAsync>d__68 stateMachine = new <WriteMaskAsync>d__68();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.andMask = andMask;
        //    stateMachine.orMask = orMask;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteOneRegisterAsync>d__69))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteOneRegisterAsync(string address, short value)
        //{
        //    <WriteOneRegisterAsync>d__69 stateMachine = new <WriteOneRegisterAsync>d__69();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteOneRegisterAsync>d__70))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteOneRegisterAsync(string address, ushort value)
        //{
        //    <WriteOneRegisterAsync>d__70 stateMachine = new <WriteOneRegisterAsync>d__70();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("ReadBoolArray", "")]
        public override OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            return ModbusHelper.ReadBoolHelper(this, address, length, 1);
        }

        [HslMqttApi("WriteBoolArray", "")]
        public override OperateResult Write(string address, bool[] values)
        {
            return ModbusHelper.Write(this, address, values);
        }

        [HslMqttApi("WriteBool", "")]
        public override OperateResult Write(string address, bool value)
        {
            return ModbusHelper.Write(this, address, value);
        }

        //[AsyncStateMachine(typeof(<ReadBoolHelperAsync>d__74))]
        //[DebuggerStepThrough]
        //private Task<OperateResult<bool[]>> ReadBoolHelperAsync(string address, ushort length, byte function)
        //{
        //    <ReadBoolHelperAsync>d__74 stateMachine = new <ReadBoolHelperAsync>d__74();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.function = function;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadBoolAsync>d__75))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
        //{
        //    <ReadBoolAsync>d__75 stateMachine = new <ReadBoolAsync>d__75();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__76))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, bool[] values)
        //{
        //    <WriteAsync>d__76 stateMachine = new <WriteAsync>d__76();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__77))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, bool value)
        //{
        //    <WriteAsync>d__77 stateMachine = new <WriteAsync>d__77();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public override string ToString()
        {
            return String.Format("ModbusTcpNet[{0}:{1}]", IpAddress, Port);
        }
    }
}
